package admin

import (
	"duoke/model"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/request"
	"duoke/utils/response"
	"fmt"
	"github.com/gin-gonic/gin"
)

var Statistics = new(statisticsCtl)

type statisticsCtl struct{}

func (ctl *statisticsCtl) Course(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.Int(req["id"])
	var where string
	if id == 0 {
		where = fmt.Sprintf("id=%d", id)
	}
	statistics, err := service.CourseList.Statistics(c, where)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "获取成功", statistics)
	return
}
func (ctl *statisticsCtl) CourseList(c *gin.Context) {
	var req model.ReqList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	validTypes := map[string]bool{"share": true, "collect": true, "buy": true, "number_of_learners": true}
	if !validTypes[req.Type] {
		response.Error(c, "类型错误【type】", nil)
		return
	}
	if req.FromId == "" {
		response.Error(c, "参数缺失【from_id】", nil)
		return
	}
	list, count, err := service.Statistics.CourseList(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", gin.H{"total": count, "list": list})
}
